<?php

namespace app\admin\controller;


use think\Exception;

class News extends Base
{
    public function index()
    {
        //模式一
//        $news = model('News')->getNews();

        $data = input('param.');
        $query = http_build_query($data);
        $whereData = [];


        //模式二

        // 转换查询条件
        if (!empty($data['start_time']) && !empty($data['end_time'])
            && $data['end_time'] > $data['start_time']
        ) {
            $whereData['create_time'] = [
                ['gt', strtotime($data['start_time'])],
                ['lt', strtotime($data['end_time'])],
            ];
        }
        if (!empty($data['catid'])) {
            $whereData['catid'] = intval($data['catid']);
        }
        if (!empty($data['title'])) {
            $whereData['title'] = ['like', '%' . $data['title'] . '%'];
        }

        $this->getPageAndSize($data);


        //获取表数据
        $news = model('News')->getNewsByCondition($whereData, $this->from, $this->size);
        //获取满足条件的数据总数=》有多少页
        $total = model('News')->getNewsCountByCondition($whereData);
        /// 结合总数+size  =》 有多少页
        ///
        $pageTotal = ceil($total / $this->size);//1.1 =>2


        return $this->fetch('', [
            'news' => $news,
            'pageTotal' => $pageTotal,
            'curr' => $this->page,
            'cats' => config('cat.lists'),
            'start_time' => !empty($data['start_time']) ? $data['start_time'] : '',
            'end_time' => !empty($data['end_time']) ? $data['end_time'] : '',
            'catid' => !empty($data['catid']) ? $data['catid'] : '',
            'title' => !empty($data['title']) ? $data['title'] : '',
            'query' => $query
        ]);
    }

    /**
     * 添加功能
     * @return mixed|void
     */
    public function add()
    {
        if (request()->isPost()) {
            $data = input('post.');

            $validate = validate('News');
            if (!$validate->check($data)) {
                return $this->error($validate->getError());
            }

            //入库操作
            try {
                $id = model('News')->add($data);
            } catch (\Exception $e) {
                return $this->result('', 0, $this->error($e->getMessage()));
            }

            if ($id) {
                return $this->result(['jump_url' => url('news/index')], 1, 'OK');
            } else {
                return $this->result('', 0, '新增失败');
            }

        } else {
            return $this->fetch('', [
                'cats' => config('cat.lists')
            ]);
        }
    }


    /**
     * 编辑功能
     * @return mixed|void
     */
    public function edit()
    {
        if (request()->isPost()) {
            $data = input('param.');
            $post = input('post.');
            $validate = validate('News');
            if (!$validate->check($post)) {
                return $this->error($validate->getError());
            }

            // 通过id 去库中查询下记录是否存在

            try {
                $user = model('News')->get(['id' => $data['id']]);
            } catch (\Exception $e) {
                return $this->result('', 0, $e->getMessage());
            }

            try {
                $res = model('News')->save($post, ['id' => $data['id']]);
            } catch (\Exception $e) {
                return $this->result('', 0, $e->getMessage());
            }

            if ($res) {
                return $this->result(['jump_url' => url('news/index')], 1, 'OK');
            }
            return $this->result('', 0, '编辑失败');


        } else {
            $data = input('param.');
            $news = model('News')->get(['id' => $data['id']]);
            return $this->fetch('', [
                'cats' => config('cat.lists'),
                'news' => $news
            ]);
        }

    }


}
